package dxunderground.winampController.Client.GUI;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.FileWriter;
import java.io.IOException;

import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import dxunderground.winampController.Common.GUI;

public class WindowActions implements WindowListener {
	private GUI gui;
	private Element properties;

	public WindowActions(GUI gui, Element properties) {
		this.gui = gui;
		this.properties = properties;

		if (properties != null) {
			Element location = properties.getChild("Location");
			Element size = properties.getChild("Size");

			Element x = location.getChild("x");
			Element y = location.getChild("y");
			gui.setLocation(this.getInt(x), this.getInt(y));

			x = size.getChild("x");
			y = size.getChild("y");
			gui.setSize(this.getInt(x), this.getInt(y));
		}
	}

	private int getInt(Element temp) {
		int num = 0;
		if (temp != null) {
			num = Integer.parseInt(temp.getText());
		}
		return num;
	}

	@Override
	public void windowActivated(WindowEvent e) {
	}

	@Override
	public void windowClosed(WindowEvent e) {
		((clientGUI) this.gui).quit();
	}

	@Override
	public void windowClosing(WindowEvent e) {
		Point point = gui.getLocationOnScreen();
		Dimension size = gui.getSize();
		if (properties == null) {
			properties = new Element("Prefrences");
		}
		Element location = properties.getChild("Location");
		if (location == null) {
			location = new Element("Location");
			properties.addContent(location);
		}
		Element locationX = location.getChild("x");
		if (locationX == null) {
			locationX = new Element("x");
			location.addContent(locationX);
		}
		locationX.setText((int) point.getX() + "");
		Element locationY = location.getChild("y");
		if (locationY == null) {
			locationY = new Element("y");
			location.addContent(locationY);
		}
		locationY.setText((int) point.getY() + "");

		Element sizeElem = properties.getChild("Size");
		if (sizeElem == null) {
			sizeElem = new Element("Size");
			properties.addContent(sizeElem);
		}
		Element sizeX = sizeElem.getChild("x");
		if (sizeX == null) {
			sizeX = new Element("x");
			sizeElem.addContent(sizeX);
		}
		sizeX.setText((int) size.getWidth() + "");
		Element sizeY = sizeElem.getChild("y");
		if (sizeY == null) {
			sizeY = new Element("y");
			sizeElem.addContent(sizeY);
		}
		sizeY.setText((int) size.getHeight() + "");
		XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
		try {
			FileWriter fout = new FileWriter("/Program Files/Winamp Controller/Client Preferences.xml");
			out.output(properties, fout);
			fout.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
	}

	@Override
	public void windowIconified(WindowEvent e) {
	}

	@Override
	public void windowOpened(WindowEvent e) {
	}
}